package com.zb.mobile.network.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.entity.*;
import com.zb.extend.StoreGoodsStockExtend;
import com.zb.mobile.NetworkMobileStoreGoodsListVo;
import com.zb.mobile.NetworkMobileStoreListVo;
import com.zb.mobile.network.service.ConNetworkStoreMobileService;
import com.zb.result.PageUtil;
import com.zb.service.*;
import com.zb.view.ViewGoods;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author WEN
 * @Date 2024/3/12 0012
 **/
@Service
public class ConNetworkStoreMobileServiceImpl implements ConNetworkStoreMobileService {
    @Resource
    private ZjNetworkEmpRelationService networkEmpRelationService;
    @Resource
    private ZjStoreService storeService;
    @Resource
    private ZjStoreGoodsStockService storeGoodsStockService;
    @Resource
    private ZjStoreInboundMaterialService storeInboundMaterialService;
    @Resource
    private ZjStoreOutboundMaterialService storeOutboundMaterialService;
    @Resource
    private ViewGoodsService viewGoodsService;

    // 获取服务商个人仓列表
    @Override
    public Map<String, Object> getNetworkMobileStoreList(AccounLoginInfo loginInfo, SelectVo selectVo) {
        ZjNetworkEmpRelation networkEmpRelation = networkEmpRelationService.getNetworkEmpIdAndNetworkId(loginInfo.getNetworkEmpId(), loginInfo.getNetworkId());
        Long networkId = loginInfo.getNetworkId() != 0 && (networkEmpRelation.getManager() || networkEmpRelation.getSuperManager()) ? loginInfo.getNetworkId() : null;

        PageUtil.setPageLimitMobile(selectVo);
        List<ZjStore> storeList = storeService.getNetworkMobileStoreList(loginInfo.getCustAccId(), networkId, loginInfo.getNetworkEmpId(), selectVo);
        List<ZjStoreGoodsStock> storeGoodsStockList = storeGoodsStockService.getByStoreIds(storeList.stream().map(ZjStore::getId).toList());

        List<Long> storeIds = storeList.stream().map(ZjStore::getId).toList();
        List<Long> goodsIds = storeGoodsStockList.stream().map(ZjStoreGoodsStock::getGoodsId).distinct().toList();
        List<Long> goodsSpecIds = storeGoodsStockList.stream().map(ZjStoreGoodsStock::getGoodsSpecId).distinct().toList();
        List<ZjStoreInboundMaterial> inboundMaterialList = storeInboundMaterialService.getInStoreQtyByStoreIdGroupByStoreId(storeIds, goodsIds, goodsSpecIds);
        List<ZjStoreOutboundMaterial> outboundMaterialList = storeOutboundMaterialService.getOutStoreQtyByStoreIdGroupByStoreId(storeIds, goodsIds, goodsSpecIds);
        return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), storeService.getNetworkMobileStoreListCount(loginInfo.getCustAccId(), networkId, loginInfo.getNetworkEmpId(), selectVo),
                storeList.stream().map(store -> {
                    NetworkMobileStoreListVo vo = new NetworkMobileStoreListVo();
                    BeanUtil.copyProperties(store, vo);
                    vo.setStatus(store.getDisabled());
                    vo.setAvailableStock(new BigDecimal(storeGoodsStockList.stream().filter(f -> Objects.equals(f.getStoreId(), store.getId()))
                            .map(ZjStoreGoodsStock::getSystemStock).reduce(Long::sum).orElse(0L).toString())
                            .add(new BigDecimal(inboundMaterialList.stream().filter(f -> Objects.equals(f.getStoreId(), store.getId()))
                                    .map(ZjStoreInboundMaterial::getQty).reduce(Long::sum).orElse(0L).toString()))
                            .subtract(new BigDecimal(outboundMaterialList.stream().filter(f -> Objects.equals(f.getStoreId(), store.getId()))
                                    .map(ZjStoreOutboundMaterial::getQty).reduce(Long::sum).orElse(0L).toString()))
                            .longValue());
                    return vo;
                }).toList());
    }

    // 获取服务商仓库商品明细列表
    @Override
    public Map<String, Object> getNetworkMobileStoreGoods(Long storeId, SelectVo selectVo) {
        PageUtil.setPageLimit(selectVo);
        long count = storeGoodsStockService.getNetworkMobileStoreGoodsCount(storeId, selectVo);
        if (count <= 0) {
            return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), count, new ArrayList<>());
        }
        List<StoreGoodsStockExtend> goodsStockExtendList = storeGoodsStockService.getNetworkMobileStoreGoods(storeId, selectVo);
        return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), count, goodsStockExtendList.stream().map(stock -> {
            NetworkMobileStoreGoodsListVo vo = new NetworkMobileStoreGoodsListVo();
            BeanUtil.copyProperties(stock, vo);
            return vo;
        }).toList());
    }
}
